//
//  MKLineComicBrowseViewController.m
//  ManKeCartoon
//
//  Created by 邹功梁 on 15/10/22.
//  Copyright © 2015年 ManKe. All rights reserved.
//

#import "MKLineComicBrowseViewController.h"

@interface MKLineComicBrowseViewController ()<UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UIView *statusBarView;

@end

@implementation MKLineComicBrowseViewController
- (void)viewWillAppear:(BOOL)animated
{
    _statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    [_statusBarView setBackgroundColor:[UIColor whiteColor]];
    [self.navBarView addSubview:_statusBarView];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT - 20)];
    _webView.delegate = self;
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]];
    
    [self.view insertSubview:_webView atIndex:0];
    
    if ([_url containsString:@"app_topic_common"]) {
        [self customNavBar];
        [_webView setFrame:CGRectMake(0, 20, SCREEN_WIDTH, SCREEN_HEIGHT - 20 + 58)];
        _webView.scrollView.bounces = NO;
    }
    else
    {
        self.navBarView.hidden = YES;
    }
    
}
#pragma mark - CustomNavBar
- (void)customNavBar
{
    self.navBarView.backgroundColor = [UIColor clearColor];
    
    _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backButton setFrame:CGRectMake(10, 23, 30, 30)];
    [_backButton setImage:[UIImage imageNamed:@"iconfont-arrowleft.png"] forState:UIControlStateNormal];
    _backButton.layer.masksToBounds = YES;
    _backButton.layer.cornerRadius = 15.;
    _backButton.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.3];
    [_backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.navBarView addSubview:_backButton];
    
}
#pragma mark - 返回按钮点击方法
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"[Error] %@", error);
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
    if ([currentURL isEqualToString:_url]) {
//        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//    http://m.ac.qq.com/Comic/ComicInfo/id/532212 开始阅读
//    https://itunes.apple.com/us/app/teng-xun-wei-man/id569387346?ls=1&mt=8 免费下载
//    NSLog(@"%@", request.URL.absoluteString);
    if ([request.URL.absoluteString containsString:@"light_comic/recommend.html"] || [request.URL.absoluteString containsString:@"comicInfo.html?"]) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if ([request.URL.absoluteString containsString:@"openmobile.qq.com"])
    {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该功能暂未开启" preferredStyle:UIAlertControllerStyleActionSheet];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
        return NO;
    }
    else if ([request.URL.absoluteString containsString:@"Comic/ComicInfo"])
    {
        MKCartoonViewController *cartoon = [[MKCartoonViewController alloc] init];
//        NSLog(@"%ld", request.URL.absoluteString.length);
        NSRange range = [request.URL.absoluteString rangeOfString:@"id/"];
//        NSLog(@"%ld", range.location);
        cartoon.comic_id = [request.URL.absoluteString substringFromIndex:range.location + range.length];
//        NSLog(@"%@", cartoon.comic_id);
        [self.navigationController pushViewController:cartoon animated:YES];
        return NO;
    }
    else if ([request.URL.absoluteString containsString:@"ui.ptlogin2.qq.com"])
    {
        [_webView goBack];
        return NO;
    }
    return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//    NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
//    NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerHTML"];
//    NSLog(@"currentURL == %@", currentURL);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
